package vip.xiaonuo.supplier.modular.finance.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.finance.account.entity.SupplierAccount;
import vip.xiaonuo.supplier.modular.finance.account.mapper.SupplierAccountMapper;
import vip.xiaonuo.supplier.modular.finance.account.param.SupplierAccountAddParam;
import vip.xiaonuo.supplier.modular.finance.account.param.SupplierAccountEditParam;
import vip.xiaonuo.supplier.modular.finance.account.param.SupplierAccountIdParam;
import vip.xiaonuo.supplier.modular.finance.account.param.SupplierAccountPageParam;
import vip.xiaonuo.supplier.modular.finance.account.service.SupplierAccountService;

import java.util.List;

/**
 * 结算账户Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/03/17 17:17
 **/
@Service
public class SupplierAccountServiceImpl extends ServiceImpl<SupplierAccountMapper, SupplierAccount> implements SupplierAccountService {

    @Override
    public Page<SupplierAccount> page(SupplierAccountPageParam supplierAccountPageParam) {
        QueryWrapper<SupplierAccount> queryWrapper = new QueryWrapper<SupplierAccount>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(supplierAccountPageParam.getAccountStore())) {
            queryWrapper.lambda().eq(SupplierAccount::getAccountStore, supplierAccountPageParam.getAccountStore());
        }
        if(ObjectUtil.isNotEmpty(supplierAccountPageParam.getAccountAccountType())) {
            queryWrapper.lambda().eq(SupplierAccount::getAccountAccountType, supplierAccountPageParam.getAccountAccountType());
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierAccount::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierAccountPageParam.getSortField(), supplierAccountPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierAccountPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierAccountPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierAccountPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierAccount::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierAccountAddParam supplierAccountAddParam) {
        SupplierAccount supplierAccount = BeanUtil.toBean(supplierAccountAddParam, SupplierAccount.class);
        //数据归属
        supplierAccount.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierAccount);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierAccountEditParam supplierAccountEditParam) {
        SupplierAccount supplierAccount = this.queryEntity(supplierAccountEditParam.getId());
        BeanUtil.copyProperties(supplierAccountEditParam, supplierAccount);
        this.updateById(supplierAccount);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierAccountIdParam> supplierAccountIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierAccountIdParamList, SupplierAccountIdParam::getId));
    }

    @Override
    public SupplierAccount detail(SupplierAccountIdParam supplierAccountIdParam) {
        return this.queryEntity(supplierAccountIdParam.getId());
    }

    @Override
    public SupplierAccount queryEntity(String id) {
        SupplierAccount supplierAccount = this.getById(id);
        if(ObjectUtil.isEmpty(supplierAccount)) {
            throw new CommonException("结算账户不存在，id值为：{}", id);
        }
        return supplierAccount;
    }
}
